﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
using System.Collections;

namespace BaHuan
{
    public partial class FrmTaiKhoanKeToan : Form
    {        
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private bool IsEdited = false;

        #region Xu ly Load

        public FrmTaiKhoanKeToan()
        {
            InitializeComponent();
        }
                
        private void FrmTaiKhoanKeToan_Load(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
            lblID.Hide();
            BindingToGrid();
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        public void BindingToGrid()
        {
            dgvTKKeToan.CanExpandGetter = delegate(object x) 
            {
                var list = (from a in GetAllTaiKhoanKeToan()
                            where a.TKCha == ((TaiKhoanKeToan)x).MaSoTK
                            select a);

                if (list.ToList().Count > 0)
                    return true;
                else
                    return false;
            };

            dgvTKKeToan.ChildrenGetter = delegate(object x)
            {
                return (from a in GetAllTaiKhoanKeToan()
                        where a.TKCha == ((TaiKhoanKeToan)x).MaSoTK
                        select a).ToList();
            };

            var lst = (from a in GetAllTaiKhoanKeToan()
                       where a.TKCha == ""
                       select a);

            dgvTKKeToan.Roots = lst.ToList();
        }

        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtTKChinh.ReadOnly = true;
                txtSoTKChinh.ReadOnly = true;
                txtSoDuBD.ReadOnly = true;
                txtSoTKCha.ReadOnly = true;
            }
            else
            {
                txtTKChinh.ReadOnly = false;
                txtSoTKChinh.ReadOnly = false;
                txtSoDuBD.ReadOnly = false;
                txtSoTKCha.ReadOnly = false;
            }
        }

        public void ClearTextBox()
        {
            txtTKChinh.Clear();
            txtSoTKChinh.Clear();
            txtSoDuBD.Clear();
            txtSoTKCha.Clear();
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();
            
            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan>  lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        #endregion
        
        #region Xu ly cac Button

        private void btnThem_Click(object sender, EventArgs e)
        {
            ClearTextBox();
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            IsEdited = false;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            txtTKChinh.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtSoTKCha.Text == "" || txtSoTKChinh.Text == "" || txtTKChinh.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin !", "Thông báo");
            }
            else
            {
                btnThem.BackColor = Color.Empty;
                btnSua.BackColor = Color.Empty;
                ResultObj result = new ResultObj();

                string sten = txtTKChinh.Text;
                string ssoTKChinh = txtSoTKChinh.Text;
                string ssoTKCha = txtSoTKCha.Text;
                long lsoDuBD = Convert.ToInt64(txtSoDuBD.Text);

                TaiKhoanKeToan taikhoanketoan = new TaiKhoanKeToan();
                taikhoanketoan.TenTK = sten;
                taikhoanketoan.MaSoTK = ssoTKChinh;
                taikhoanketoan.TKCha = ssoTKCha;
                taikhoanketoan.SoDuBanDau = lsoDuBD;

                if (!IsEdited)
                {
                    result = taikhoanketoanMgt.SaveNewTaiKhoanKeToan(taikhoanketoan);
                }
                else
                {
                    taikhoanketoan.Id = Convert.ToInt32(lblID.Text);
                    result = taikhoanketoanMgt.UpdateTaiKhoanKeToan(taikhoanketoan);
                    IsEdited = false;
                }
                if (result.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    FrmTaiKhoanKeToan_Load(null, null);
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTKChinh.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTKChinh.Select();
                }

                BindingToGrid();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            IsEdited = true;
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            btnLuu.Enabled = true;
            txtTKChinh.Select();
        }

        #endregion

        #region Xu ly cac su kien

        private void dgvTKKeToan_CellClick(object sender, BrightIdeasSoftware.CellClickEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                ChangeStatusTextBox(true);
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                btnXoa.Enabled = true;

                txtSoTKChinh.Text = dgvTKKeToan.Items[e.RowIndex].SubItems[0].Text; 
                txtTKChinh.Text = dgvTKKeToan.Items[e.RowIndex].SubItems[1].Text;                               
                txtSoDuBD.Text = dgvTKKeToan.Items[e.RowIndex].SubItems[2].Text;

                TaiKhoanKeToan tk = (from a in GetAllTaiKhoanKeToan()
                                     where a.MaSoTK == txtSoTKChinh.Text
                                     select a).FirstOrDefault();

                lblID.Text = tk.Id.ToString();
                txtSoTKCha.Text = tk.TKCha;
            }
        }

        private void txtSoDuBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }

        private void txtSoDuHT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }

        private void txtSoTKChinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }

        private void txtSoTKCha_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }

        private void txtSoTKCha_Leave(object sender, EventArgs e)
        {
            var lst = (from a in GetAllTaiKhoanKeToan()
                       where a.MaSoTK == txtSoTKCha.Text
                       select a);

            if (lst.ToList().Count == 0 && txtSoTKCha.Text != "000")
            {
                MessageBox.Show("Vui lòng kiểm tra lại tài khoản cha!\r\n***Tài khoản cha sẽ được ký hiệu bằng 000");
                txtSoTKCha.Focus();
            }
        }

        #endregion
    }
}
